library(XML)
library(stringr)
compProdKey <- list(salesForce=c('sfdc','salesforce'))
#                    ,seibel=c('siebel','siebel crm')
#                    ,dynamics=c('MS dynamics'))
companyData <- data.frame(product=NULL,company=NULL,jobkey=NULL,location=NULL,postDate=NULL)

for (p in seq(length(compProdKey))){
  anyQ <- str_c(compProdKey[[p]],collapse='+')
  print(anyQ)
  count <- xmlParse(str_c('http://api.indeed.com/ads/apisearch?',
                        'publisher=2487201386265292&',
                        'q=%28sfdc+or+salesforce%29&start=&l=&v=2'))
  countDF <- xmlToDataFrame(getNodeSet(count, "//totalresults"),stringsAsFactors=FALSE)
  print(countDF$text)

  for(i in 1:countDF$text/25) {
    
    startingJobIndex <- i*25
    print(str_c('getting job details at job index ',startingJobIndex))
    
    url <- str_c('http://api.indeed.com/ads/apisearch?',
                 'publisher=2487201386265292&',
                 'q=%28sfdc+or+salesforce%29&l=&limit=25&start=',startingJobIndex,'&v=2')
    
    tryCatch( {
    results <- xmlParse(url)
    data <- xmlToDataFrame(getNodeSet(results, "//result"),stringsAsFactors=FALSE)   
    companyData <- rbind(companyData,data.frame(product=names(compProdKey)[p],
                                                company=data$company,data$jobkey,
                                                location=data$formattedLocation,
                                                date=data$date))
    write.csv(companyData,file='c:/salesinsight/dataFiles/salesforce.csv',append=TRUE)
    
    }, error = function(e) {
      print(str_c('errorgetting urlat job index ', startingJobIndex))
      print(url)
    }
    )
    
  
    }
}
